package main.Q1701_1800;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Q1771_1780 {
    public static void main(String[] args) {
        System.out.println("Question1771：");
        System.out.println("Question1772：");
        System.out.println("Question1773：统计匹配检索规则的物品数量");
        System.out.println("Question1774：");
        System.out.println("Question1775：");
        System.out.println("Question1776：");
        System.out.println("Question1777：");
        System.out.println("Question1778：");
        System.out.println("Question1779：找到最近的有相同的X或Y坐标的点");
        System.out.println("Question1780：判断一个数字是否可以表示成三的幂的和");
        System.out.println(new Question1780().checkPowersOfThree(27));
    }
}

class Question1773 {
    public int countMatches(List<List<String>> items, String ruleKey, String ruleValue) {
        int index = 0, result = 0;
        if (ruleKey.equals("color")) index = 1;
        else if (ruleKey.equals("name")) index = 2;
        for (List<String> item : items) {
            if (item.get(index).equals(ruleValue)) result++;
        }
        return result;
    }
}

class Question1779 {
    public int nearestValidPoint(int x, int y, int[][] points) {
        int minDistance = Integer.MAX_VALUE, index = -1;
        for (int i = 0; i < points.length; i++) {
            if (points[i][0] == x || points[i][1] == y) {//有效
                int distance = Math.abs(points[i][0] - x) + Math.abs(points[i][1] - y);
                if (distance < minDistance) {
                    minDistance = distance;
                    index = i;
                }
            }
        }
        return index;
    }
}

class Question1780 {
    public boolean checkPowersOfThree(int n) {
        int multi = 1, i = 0;
        Set<Integer> set = new HashSet<>();
        while (multi * 3 <= n) {
            multi *= 3;
            i++;
        }
        set.add(i);
        n = n - multi;
        while (n > 0) {
            while (multi > n) {
                multi /= 3;
                i--;
            }
            if (set.contains(i)) {
                return false;
            }
            n -= multi;
            set.add(i);
        }
        return true;
    }
}